package com.learnyou.server.xingzuo.biz.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import com.learnyou.server.xingzuo.common.exception.LearnyouBootException;
import com.learnyou.server.xingzuo.biz.common.CosFileBiz;
import com.learnyou.server.xingzuo.biz.helper.CosHelper;
import com.learnyou.server.xingzuo.biz.helper.MediaSaveObject;
import com.learnyou.server.xingzuo.common.config.CosConfig;
import com.learnyou.server.xingzuo.common.constants.ErrorCode;
import com.learnyou.server.xingzuo.common.util.ImageUtils;
import com.qcloud.cos.model.COSObjectInputStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CosFileBizImpl implements CosFileBiz {

    @Resource
    private CosConfig cosConfig;

    @Override
    public COSObjectInputStream readFile(String target) {
        CosHelper cosHelper = new CosHelper(cosConfig.getSecretId(), cosConfig.getSecretKey(), cosConfig.getRegion()).bucket(cosConfig.getBucketName());
        try {
            return cosHelper.readObject(target);
        } catch (Exception ex) {
            throw new LearnyouBootException(ErrorCode.FILE_READ_ERROR);
        }
    }

    @Override
    public String saveFile(String saveAs, InputStream stream, long size) {
        CosHelper cosHelper = new CosHelper(cosConfig.getSecretId(), cosConfig.getSecretKey(), cosConfig.getRegion())
                .bucket(cosConfig.getBucketName());
        try {
            return cosHelper.saveObject(saveAs, stream, size);
        } catch (Exception ex) {
            throw new LearnyouBootException(ErrorCode.FILE_WRITE_ERROR);
        }
    }

    @Override
    public String saveFile(String saveAs, String file) {
        CosHelper cosHelper = new CosHelper(cosConfig.getSecretId(), cosConfig.getSecretKey(), cosConfig.getRegion())
                .bucket(cosConfig.getBucketName());
        return cosHelper.saveObject(saveAs, file);
    }

    @Override
    public List<String> saveFile(List<MediaSaveObject> mediaSaveObjects) {
        CosHelper cosHelper = new CosHelper(cosConfig.getSecretId(), cosConfig.getSecretKey(), cosConfig.getRegion())
                .bucket(cosConfig.getBucketName());
        try {
            return cosHelper.saveObject(mediaSaveObjects);
        } catch (Exception ex) {
            throw new LearnyouBootException(ErrorCode.FILE_WRITE_ERROR);
        }
    }

    @Override
    public boolean removeFile(String target) {
        try {
            CosHelper cosHelper = new CosHelper(cosConfig.getSecretId(), cosConfig.getSecretKey(), cosConfig.getRegion())
                    .bucket(cosConfig.getBucketName());
            cosHelper.removeObject(target);
            return true;
        } catch (Exception ex) {
            throw new LearnyouBootException(ErrorCode.FILE_REMOVE_ERROR);
        }
    }

    @Override
    public List<String> saveImageFile(List<MediaSaveObject> mediaSaveObjects) {
        List<MediaSaveObject> origin = new ArrayList<>();
        List<MediaSaveObject> scaleList = new ArrayList<>();
        for (MediaSaveObject mediaSaveObject : mediaSaveObjects) {
            try {
                byte[] bytes = IOUtils.toByteArray(mediaSaveObject.getInputStream());
                mediaSaveObject.setInputStream(new ByteArrayInputStream(bytes));
                origin.add(mediaSaveObject);
                MediaSaveObject scale = new MediaSaveObject();
                byte[] scaleBytes = ImageUtils.resizeImage(bytes);
                scale.setInputStream(new ByteArrayInputStream(scaleBytes));
                scale.setSize(scaleBytes.length);
                String fileName = mediaSaveObject.getSaveTo();
                scale.setSaveTo("scale_"+fileName);
                scaleList.add(scale);
            } catch (IOException e) {
                log.error("文件读取异常", e);
            }

        }
        List<String> rs = saveFile(origin);
        saveFile(scaleList);
        return rs;
    }

    @Override
    public String getDomain() {
        return cosConfig.getDomain();
    }
}
